package joney.main.analysis;

import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import joney.main.views.report.TimeRangeChart;
import joney.model.Account;
import joney.model.DataModel;
import joney.model.Expense;

public class ExpenseUtils {

	public static Vector<Expense> getAllExpenseOfTypeT(String account, String type)
	{
		Vector<Expense> es = new Vector<Expense>();
		
		for(Account a: DataModel.accounts)
		{
			if(a.getName().equals(account))
			{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(type))
					{
						es.add(e);
					}
				}
			}
		}
		
		return es;
	}
	
	public static Vector<Expense> getAllExpenseOfNameT(String account, String name)
	{
		Vector<Expense> es = new Vector<Expense>();
		String namel = name.toLowerCase();
		
		for(Account a: DataModel.accounts)
		{
			if(a.getName().equals(account))
			{
				for(Expense e: a.getExpenses())
				{
					if(e.getName().equals(namel))
					{
						es.add(e);
					}
				}
			}
		}
		
		return es;
	}
	
	//from all accounts
	public static Vector<Expense> getAllExpenseOfTypeT(String type)
	{
		Vector<Expense> es = new Vector<Expense>();
		
		for(Account a: DataModel.accounts)
		{
			for(Expense e: a.getExpenses())
			{
				if(e.getType().equals(type))
				{
					es.add(e);
				}
			}
		}
		
		return es;
	}
	
	//from all accounts
	public static Vector<Expense> getAllExpenseOfNameT(String name)
	{
		Vector<Expense> es = new Vector<Expense>();
		
		String namel = name.toLowerCase();
		for(Account a: DataModel.accounts)
		{
			for(Expense e: a.getExpenses())
			{
				if(e.getName().toLowerCase().equals(namel))
				{
					es.add(e);
				}
			}
		}
		
		return es;
	}
	
	public static double getTotalTypeAmount(String account, String type)
	{
		double totalAmount = 0;
		
		for(Account a: DataModel.accounts)
		{
			if(a.getName().equals(account))
			{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(Expense.TRANSFER)){continue;}
					
					if(type.equalsIgnoreCase(e.getType()))
					{
						totalAmount+=e.getAmount();
					}
				}
			}
		}
		
		return totalAmount;
	}
	
	public static Hashtable<String, Double> getAllTypeAmount(String account)
	{
		Hashtable<String, Double> total = new Hashtable<String, Double>();
		
		for(Account a: DataModel.accounts)
		{
			if(a.getName().equals(account))
			{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(Expense.TRANSFER)){continue;}
					
					if(total.containsKey(e.getType()))
					{
						total.put(e.getType() , total.get(e.getType())+ e.getAmount());
					}
					else
						total.put(e.getType(), e.getAmount());
				}
			}
		}
		
		return total;
	}
	
	
	
	public static Hashtable<String, Double> getAllTypeAmount()
	{
		Hashtable<String, Double> total = new Hashtable<String, Double>();
		
		
		for(Account a: DataModel.accounts)
		{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(Expense.TRANSFER)){continue;}
					
					if(total.containsKey(e.getType()))
					{
						total.put(e.getType() , total.get(e.getType())+ e.getAmount());
						
					}
					else
						total.put(e.getType(), e.getAmount());
						
				}
		}
		
		return total;
	}
	
	public static Vector<DayTotal> getTotalExpenseForDays(String accountName, int monthStart, int dayStart, int yearStart, 
			int monthStop, int dayStop, int yearStop)
	{
		Vector<DayTotal> days = new Vector<DayTotal>();
		
		Vector<Expense> expenses  = new Vector<Expense>();
		
		if(accountName.equals(TimeRangeChart.ALL))
		{
			for(Account a: DataModel.accounts)
				expenses.addAll(DateComparison.
						getAllExpenseInBetweenDates(a.getName(), monthStart, dayStart, yearStart,monthStop, dayStop, yearStop));
		}
		else{
			expenses = DateComparison.
			getAllExpenseInBetweenDates(accountName, monthStart, dayStart, yearStart,monthStop, dayStop, yearStop);
		}
		
		DayTotal currentDay = new DayTotal();
		Date currentDate=null;
		
		for(Expense e: expenses)
		{
			if(currentDate == null){
				currentDate = new Date(e.getDate());
				currentDay.day = currentDate;
			}
			
			currentDay.totalValue+=e.getAmount();
			
			Date d = new Date(e.getDate());
			//if days match
			if(d.after(currentDay.day) && d.before(currentDay.day))
			{
				days.add(currentDay);
				
				currentDate =new Date(e.getDate());
				currentDay = new DayTotal();
				currentDay.day = currentDate;
			}
		}
		
		days.add(currentDay);
		
		return days;
	}
	
	public static class DayTotal
	{
		public Date day;
		public double totalValue=0;
	}
	
	
}
